﻿using System;
using System.Drawing;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Baimasoft.Manage.SqlServerDAL;


public partial class SysManage_Role_List : System.Web.UI.Page
{
   
    public static int type = 0;
    public static string sSRoleCode = "" , sSRoleName = "";
    public string UserRole = "";
    public int AllCount = 0, AllPageCount = 0, NowPageCount = 0, PageSize = 0;
    DataView MyDV;

    RoleManage MyRM = new RoleManage();

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((string)Session["Baimasoft_User_LogonID"] == null)
        {
            Response.Redirect("../ReLogon.aspx");
        }
        UserRole = (string)Session["Baimasoft_Role_Code"];
        if (!Page.IsPostBack)
        {
            GridView1.Attributes.Add("BorderColor", "LightSkyBlue"); 

            if (ViewState["OpType"] == null)
            {
                ViewState["OpType"] = "1";
            }

            if (ViewState["SortExpression"] == null || ViewState["SortExpression"].ToString().CompareTo("") == 0)
            {
                ViewState["SortExpression"] = "Code";
            }

            if (ViewState["SortDirection"] == null || ViewState["SortDirection"].ToString().CompareTo("") == 0)
            {
                ViewState["SortDirection"] = " ";
            }
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];

            //RoleBind(UserRole, sSqlSort);
            Bind(UserRole, sSqlSort);
        }
    }

    //默认数据绑定
    public void RoleBind(string sUserRole, string SqlSort)
    {
        /*RoleName.DataSource = MYUM.GetMyRoleInfo(sUserRole, "1", SqlSort);
        RoleName.DataBind();
        RoleName.Items.Insert(0, "全部角色");
        RoleName.Items[0].Value = "0";*/
    }

    private void Bind(string sUserRole, string SqlSort)
    {
        if (ViewState["OpType"].ToString() == "0")
        {
            MyDV = MyRM.GetMyCtrlRole (sUserRole, "1", SqlSort).DefaultView ;
        }
        else if (ViewState["OpType"].ToString() == "1")  //查询状态
        {
            MyDV = MyRM .SearchMyCtrlRole (sSRoleCode, sSRoleName, sUserRole, SqlSort);
        }
        else if (ViewState["OpType"].ToString() == "2") // 显示删除
        {
            MyDV = MyRM.GetMyCtrlRole(sUserRole, "0", SqlSort).DefaultView ;
        }
        GridView1.DataSource = MyDV;
        GridView1.DataBind();
        AllPageCount = GridView1.PageCount;
        NowPageCount = GridView1.PageIndex + 1;
        AllCount = MyDV.Count;
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(UserRole, sSqlSort);
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {

    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {       
        if (e.Row.RowIndex != -1)
        {
            if (e.Row.RowIndex % 2 != 0)
            {
                e.Row.Attributes.Add("onclick", "GetRowInfo('" + e.Row.Cells[0].Text + "|" + e.Row.Cells[1].Text + "|" + e.Row.Cells[2].Text + "|" + e.Row.Cells[3].Text + "|" + e.Row.Cells[4].Text + "'),tog(this,'#EFEFF8')");
            }
            else
            {
                e.Row.Attributes.Add("onclick", "GetRowInfo('" + e.Row.Cells[0].Text + "|" + e.Row.Cells[1].Text + "|" + e.Row.Cells[2].Text + "|" + e.Row.Cells[3].Text + "|" + e.Row.Cells[4].Text + "'),tog(this,'#F9F9FB')");
            }
            if (e.Row.Cells[3].Text == "0")
                e.Row.ForeColor = Color.Red;
        }       
    }
    protected void BtnExport_Click(object sender, EventArgs e)
    {
        string sFileDT = string.Format("{0:yyyyMMddHHmmss}", System.DateTime.Now);
        string sFileName = "RoleInfo(" + sFileDT + ").xls";

        sSRoleCode = RoleCode.Text.ToString().Trim();
        sSRoleName = RoleName.Text.ToString().Trim(); // RoleName.SelectedItem.ToString().Trim();

        DrawExcelFromDataGrid(MyRM .ExportCtrlRole (sSRoleCode, sSRoleName), sFileName);
    }
    protected void BtnDel_Click(object sender, EventArgs e)
    {
        string sRoleCode = HRoleCode.Value.ToString().Trim();
        //MYUM.DelRoleGrantInfo(sRoleCode);
        int iback = MyRM .IFDelRoleInfo (sRoleCode, "0");
        if (iback == 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除成功!');</script>");
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(UserRole, sSqlSort);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除失败!');</script>");
        }        
    }
    protected void BtnDispDel_Click(object sender, EventArgs e)
    {
        ViewState["OpType"] = "2";
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(UserRole, sSqlSort);
    }
    protected void BtnHideDel_Click(object sender, EventArgs e)
    {
        ViewState["OpType"] = "0";
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(UserRole, sSqlSort);
    }
    protected void BtnRefresh_Click(object sender, EventArgs e)
    {
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(UserRole, sSqlSort);
    }
    protected void BtnResume_Click(object sender, EventArgs e)
    {
        string sResumeCode = HRoleCode.Value.ToString().Trim();
        int iback = MyRM.IFDelRoleInfo(sResumeCode, "1");
        if (iback == 1)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('恢复成功!');</script>");
            ViewState["OpType"] = "2";
            string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
            Bind(UserRole, sSqlSort);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('恢复失败!');</script>");
        }        
    }
    protected void SearchRoleInfo_ServerClick(object sender, EventArgs e)
    {
        ViewState["OpType"] = "1";
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        sSRoleCode = RoleCode.Text.ToString().Trim();
        sSRoleName = RoleName.Text.ToString().Trim();  // RoleName.SelectedValue.ToString().Trim();// RoleName.DataTextField.ToString().Trim();// RoleName.SelectedValue .ToString ().Trim ();
        Bind(UserRole, sSqlSort);
    }

    public string ListState(string str1)
    {
        if (str1 == "1")
            return "是";
        else
            return "<font color=red>否</font>";
    }

    /// 从GridView中导出到Excel中去
    private void DrawExcelFromDataGrid(DataView dv, string FileName)
    {
        HttpResponse resp;
        resp = Page.Response;
        resp.ContentEncoding = System.Text.Encoding.Default;
        resp.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
        string colHeaders = "", ls_item = "";

        string sHeading = "角色代码|角色名称|角色描述|是否有效";
        string sColumns = "0|1|2|3";
        string[] AHeading = sHeading.Split('|');
        int iHeading = AHeading.Length;
        for (int i = 0; i < iHeading - 1; i++)
        {
            colHeaders += AHeading[i] + Convert.ToChar(9);
        }
        colHeaders += AHeading[iHeading - 1] + Convert.ToChar(13);
        resp.Write(colHeaders);


        GridView MyGV = new GridView();
        MyGV.DataSource = dv;
        MyGV.DataBind();

        string[] AColumns = sColumns.Split('|');
        int iColumns = AColumns.Length;

        foreach (GridViewRow row in MyGV.Rows)
        {
            for (int j = 0; j < iColumns - 1; j++)
            {
                int k = Convert.ToInt32(AColumns[j]);
                ls_item = ls_item + backstring(row.Cells[k].Text.ToString().Trim()) + Convert.ToChar(9);
            }
            int p = Convert.ToInt32(AColumns[iColumns - 1]);
            ls_item = ls_item + backstring(row.Cells[p].Text.ToString().Trim()) + Convert.ToChar(13);
            resp.Write(ls_item);
            ls_item = "";
        }
        resp.End();
    }

    protected string backstring(string stmp)
    {
        if (stmp == "&nbsp;")
        {
            stmp = "";
        }
        return stmp;
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (ViewState["SortDirection"].ToString() == "desc")
        {
            ViewState["SortDirection"] = " ";
        }
        else
        {
            ViewState["SortDirection"] = "desc";
        }

        ViewState["SortExpression"] = e.SortExpression;
        string sSqlSort = " ORDER BY " + ViewState["SortExpression"] + " " + ViewState["SortDirection"];
        Bind(UserRole, sSqlSort);
    }
}
